﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApC3DTCAttachmentManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.IO;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

internal class CApC3DTCAttachmentManager : CApC3DAttachmentManager
{
  private TxFileAttachment m_u3dAttachement;
  private TxFileAttachment m_xmlAttachement;

  internal CApC3DTCAttachmentManager(CApC3DRootNodeData root)
    : base(root)
  {
  }

  internal override void Initialize()
  {
    if (this.Root == null || this.Root.PlanningNodeObject == null)
      return;
    TxFileAttachment[] fileAttachments = this.Root.PlanningNodeObject.FileAttachments;
    foreach (TxFileAttachment txFileAttachment in fileAttachments)
    {
      if (txFileAttachment.Name.EndsWith(CApC3DUtils.U3DExtension))
      {
        this.m_u3dAttachement = txFileAttachment;
        break;
      }
      if (txFileAttachment.Name.EndsWith(CApC3DUtils.PRCExtension))
      {
        this.m_u3dAttachement = txFileAttachment;
        break;
      }
    }
    if (this.m_u3dAttachement == null)
      return;
    string withoutExtension1 = Path.GetFileNameWithoutExtension(this.m_u3dAttachement.Name);
    foreach (TxFileAttachment txFileAttachment in fileAttachments)
    {
      string name = txFileAttachment.Name;
      string withoutExtension2 = Path.GetFileNameWithoutExtension(name);
      if (Path.GetExtension(name) == CApC3DUtils.DataFileExtension && withoutExtension2 == withoutExtension1)
      {
        this.m_xmlAttachement = txFileAttachment;
        string pathInFileSystem = this.m_xmlAttachement.FullPathInFileSystem;
        string path = this.Root.PlanningNodeObject.DownloadFileAttachment(this.m_xmlAttachement);
        if (this.m_xmlAttachement == null || path.Length <= 0)
          break;
        this.InitializeReader(path);
        break;
      }
    }
  }

  internal override bool DoesHave3D => this.m_u3dAttachement != null;

  private void Remove3D()
  {
    this.Root.PlanningNodeObject.RemoveFileAttachment(this.m_u3dAttachement);
    this.m_u3dAttachement = (TxFileAttachment) null;
  }

  internal override void Set3D(
    string newU3DFile,
    string newJSFile,
    string newViewDataFile,
    string newViewNodesFile)
  {
    if (this.m_u3dAttachement != null)
      this.Remove3D();
    TxFileAttachmentData fileAttachmentData1 = (TxFileAttachmentData) null;
    string withoutExtension = Path.GetFileNameWithoutExtension(newU3DFile);
    TxFileAttachmentData fileAttachmentData2 = (TxFileAttachmentData) new TxTcFileAttachmentData(newU3DFile, withoutExtension);
    TxFileAttachmentData fileAttachmentData3 = (TxFileAttachmentData) new TxTcFileAttachmentData(newViewDataFile, withoutExtension);
    TxFileAttachmentData fileAttachmentData4 = (TxFileAttachmentData) new TxTcFileAttachmentData(newViewNodesFile, withoutExtension);
    if (newJSFile != string.Empty)
      fileAttachmentData1 = (TxFileAttachmentData) new TxTcFileAttachmentData(newJSFile, withoutExtension);
    this.m_u3dAttachement = this.Root.PlanningNodeObject.AddFileAttachment(fileAttachmentData2);
    TxPlanningObjectEx planningObjectEx = new TxPlanningObjectEx(this.Root.PlanningNodeObject);
    if (planningObjectEx == null)
      return;
    this.m_xmlAttachement = planningObjectEx.AttachFileToNode(fileAttachmentData3, this.m_u3dAttachement);
    this.m_xmlAttachement = planningObjectEx.AttachFileToNode(fileAttachmentData4, this.m_u3dAttachement);
    if (fileAttachmentData1 == null)
      return;
    this.m_xmlAttachement = planningObjectEx.AttachFileToNode(fileAttachmentData1, this.m_u3dAttachement);
  }
}
